package com.wutongyu.mannyburger;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProductSpecAdapter extends RecyclerView.Adapter<ProductSpecAdapter.SpecViewHolder> {

    private Context context;
    private List<String> specList;
    private int selectedPosition = 0;

    public ProductSpecAdapter(Context context, List<String> specList) {
        this.context = context;
        this.specList = specList;
    }

    @NonNull
    @Override
    public SpecViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.product_spec_item, parent, false);
        return new SpecViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SpecViewHolder holder, int position) {
        String spec = specList.get(position);
        holder.specName.setText(spec);
        
        // 默认选中第一个规格
        holder.specCheckBox.setChecked(position == selectedPosition);
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            // 取消之前选中的位置
            int previousSelected = selectedPosition;
            selectedPosition = holder.getAdapterPosition();
            
            // 更新选中状态
            notifyItemChanged(previousSelected);
            notifyItemChanged(selectedPosition);
        });
    }

    @Override
    public int getItemCount() {
        return specList.size();
    }

    static class SpecViewHolder extends RecyclerView.ViewHolder {
        CheckBox specCheckBox;
        TextView specName;

        public SpecViewHolder(@NonNull View itemView) {
            super(itemView);
            specCheckBox = itemView.findViewById(R.id.checkbox_spec);
            specName = itemView.findViewById(R.id.text_spec_name);
        }
    }
} 